package com.szxy.frontend.portal.service.impl;

import com.szxy.frontend.portal.feign.CommonContentFeignClient;
import com.szxy.frontend.portal.feign.CommonItemsFeignClient;
import com.szxy.frontend.portal.feign.CommonRedisFeiginClient;
import com.szxy.frontend.portal.service.ContentService;
import com.szxy.pojo.TbContent;
import com.szxy.utils.ContentAD;
import com.szxy.utils.PageResult;
import com.szxy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther:zwer
 * @Date:2019/9/11 9:31
 * @Description:com.szxy.frontend.portal.service.impl
 * @Version:1.0
 **/
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private CommonContentFeignClient commonContentFeignClient;
    @Autowired
    private CommonRedisFeiginClient commonRedisFeiginClient;

    @Override
    public Result selectFrontendContentByAD() {
        //将 Redis 数据库与 Mysql 数据库分开，防止 Redis 出错，影响关系数据库查询
        //从 Redis 中查询数据
        try {
            List<ContentAD> contentADList = this.commonRedisFeiginClient.getContentAD();
            if (contentADList != null && contentADList.size() > 0) {
                return Result.ok(contentADList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 关系型数据库中查询
        List<ContentAD> contentADList = this.commonContentFeignClient.selectFrontendContentByAD();
        try {
            if (contentADList != null && contentADList.size() > 0) {
                //将首页大广告数据放入 Redis 缓存中
                this.commonRedisFeiginClient.insertContentAD(contentADList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (contentADList != null && contentADList.size() > 0) {
            return Result.ok(contentADList);
        }
        //若从 Redis 和 关系型数据库中都查询不到数据，则返回查询错误信息
        return Result.error("查询错误");
        }
    }
